extends Area2D

@export var max_speed = 100
@export var min_speed = 50

var move_speed: float = 100
var item_type: int

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var enum_keys = ItemType.ITEM_TYPE.keys()
	var random_index = randi() % enum_keys.size()
	var random_enum_name = enum_keys[random_index]
	var random_enum_value = ItemType.ITEM_TYPE[random_enum_name]
	setup(random_enum_value)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	position.y += move_speed * delta

func setup(type: int) -> void:
	item_type = type
	var texture = ItemType.get_texture(type)
	if texture != null:
		$Sprite2D.texture = texture

	move_speed = randf_range(min_speed, max_speed)
	
func stop() -> void:
	print("item ", self.name, "(", get_item_type(), ") is stopped")
	hide()
	$CollisionShape2D.disabled = true
	queue_free()

func get_item_type() -> int:
	return item_type

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	stop()

func _on_area_entered(area: Area2D) -> void:
	print(self.name, "is entered by ", area.name)
	stop()
